﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.BackgroundJobs;
using Abp.Domain.Repositories;
using Abp.Runtime.Validation.Interception;
using apbmvc.Caches;
using apbmvc.companys.Dto;
using apbmvc.Hangfire;
using apbmvc.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace apbmvc.companys
{
    public class CompanyAppService:  ApplicationService, ICompanyAppService
    {
        private  IRepository<companies, int> _repository { get; set; }
        private IBackgroundJobManager _backgroundJobManager { get; set; }

        private ISqlExecuter _isqlexecuter { get; set; }


        private readonly ICompanyCache _companyCache;

        public CompanyAppService(IRepository<companies, int> repository, ISqlExecuter sqlExecuter, 
            IBackgroundJobManager backgroundJobManager,
            ICompanyCache companyCache)
        {
            _repository = repository;
            _isqlexecuter = sqlExecuter;
            _backgroundJobManager = backgroundJobManager;
            _companyCache = companyCache;
        }


        public async Task<ListResultDto<companyDto>> GetCompanies()
        {
            var coms = await _repository.GetAllListAsync();
            return new ListResultDto<companyDto>(coms.MapTo<List<companyDto>>());

        }

        public async Task Addcompany(companyDto dto)
        {

           

            Logger.Debug("Service  my name is debug");
            Logger.Error("Service  my name is debug");
            Logger.Warn("Service  my name is debug");

            var com = dto.MapTo<companies>();
            await _repository.InsertAsync(com);
        }

        public async Task Delcompany(int id)
        {
            await _repository.DeleteAsync(id);
        }

        public List<companyDto> GetTsAsync(string sql)
        {
            var iqdata = _isqlexecuter.SqlQuery<companyDto>(sql);
            var data =  iqdata.ToList();
            return data;

        }



        public void ExTestJob()
        {
            _backgroundJobManager.Enqueue<TestJob,int>(42);
        }



        public string GetCacheCompaniesList()
        {
            var data = _companyCache.Get(3);


            _repository.Update(new companies() { Id = 3, comname = DateTime.Now.ToShortTimeString(), comnameshort = "-", natureid = 15 });


            return data.comname;

        }


    }
}
